﻿import QtQuick
import QtQuick.Layouts
import QtQuick.Window
import QtQuick.Controls
import FilUI

import "../controls"

FilScrollablePage {
    id: control

    property int rightScrollableMargin: 17
    property int frameSpacing: 12

    FilFrame {
        Layout.fillWidth: true
        Layout.rightMargin: control.rightScrollableMargin
        height: 100
        padding: 10
        color: "transparent"
        ColumnLayout {
            spacing: 5
            FilText {
                text: qsTr("Author")
                font: FilTextStyle.BodyStrong
                Layout.bottomMargin: 4
            }
            FilTextButton {
                text: "TimeChicken"
                horizontalPadding: 0
                backgroundHoverColor: FilTheme.itemNormalColor
                backgroundPressedColor: FilTheme.itemNormalColor
                onHoveredChanged: {
                    if (!hovered) {
                        FilTools.restoreOverrideCursor()
                    } else {
                        FilTools.setOverrideCursor(Qt.PointingHandCursor)
                    }
                }
                onClicked: {
                    Qt.openUrlExternally("https://gitee.com/time--chicken")
                }
            }
        }
    }
    FilFrame {
        id: id_filframe__checkupdate
        Layout.fillWidth: true
        Layout.rightMargin: control.rightScrollableMargin
        height: 100
        padding: 10
        color: "transparent"
        ColumnLayout {
            spacing: 5
            FilText {
                text: qsTr("Version")
                font: FilTextStyle.BodyStrong
                Layout.bottomMargin: 4
            }
            FilExpander {
                Layout.preferredWidth: parent
                headerHeight: 65
                contentHeight: content_layout.implicitHeight
                headerDelegate: Component {
                    Item {
                        RowLayout {
                            anchors {
                                verticalCenter: parent.verticalCenter
                                left: parent.left
                                leftMargin: 15
                            }
                            spacing: 15
                            FilImage {
                                width: 40
                                height: 40
                                sourceSize.width: 40
                                sourceSize.height: 40
                                source: "qrc:/res/image/favicon_nbg.png"
                            }
                            ColumnLayout {
                                spacing: 0
                                FilText {
                                    text: "SecAssistPro"
                                }
                                FilText {
                                    text: AppInfo.version
                                    textColor: FilTheme.fontSecondaryColor
                                    font.pixelSize: 12
                                }
                            }
                        }
                        FilLoadingButton {
                            id: btn_checkupdate
                            anchors {
                                verticalCenter: parent.verticalCenter
                                right: parent.right
                                rightMargin: 15
                            }
                            text: qsTr("Check for Updates")
                            onClicked: {
                                loading = true
                                FilEventBus.post("checkUpdate")
                            }
                        }
                        FilEvent {
                            name: "checkUpdateFinish"
                            onTriggered: {
                                btn_checkupdate.loading = false
                            }
                        }
                    }
                }
                content: ColumnLayout {
                    id: content_layout
                    spacing: 0
                    RowLayout {
                        Layout.topMargin: 10
                        Layout.leftMargin: 15
                        Layout.bottomMargin: 10
                        spacing: 0
                        FilText {
                            text: "Gitee: "
                        }
                        FilTextButton {
                            text: "https://gitee.com/time--chicken/SecAssistPro"
                            horizontalPadding: 0
                            backgroundHoverColor: FilTheme.itemNormalColor
                            backgroundPressedColor: FilTheme.itemNormalColor
                            onHoveredChanged: {
                                if (!hovered) {
                                    FilTools.restoreOverrideCursor()
                                } else {
                                    FilTools.setOverrideCursor(Qt.PointingHandCursor)
                                }
                            }
                            onClicked: {
                                Qt.openUrlExternally(text)
                            }
                        }
                    }
                }
            }
            FilTextButton {
                text: qsTr("Have a problem?")
                backgroundHoverColor: FilTheme.itemNormalColor
                backgroundPressedColor: FilTheme.itemNormalColor
                onHoveredChanged: {
                    if (!hovered) {
                        FilTools.restoreOverrideCursor()
                    } else {
                        FilTools.setOverrideCursor(Qt.PointingHandCursor)
                    }
                }
                onClicked: {
                    Qt.openUrlExternally("https://gitee.com/time--chicken/SecAssistPro/issues?q=is:all")
                }
            }
        }
        FilEvent {
            name: "checkUpdate"
            onTriggered: {
                id_filframe__checkupdate.checkUpdate(false)
            }
        }
        FilContentDialog {
            property string newVersion
            property string body
            id: id_filcontentdialog__update
            title: qsTr("Upgrade Tips")
            implicitWidth: 520
            message: qsTr("SecAssistPro is currently up to date ") +
                     newVersion +
                     qsTr(" -- The current app version") + AppInfo.version + " \n" +
                     qsTr("Now go and download the new version?") + "\n\n" +
                     qsTr("Updated content: ") + "\n" + body
            buttonFlags: FilContentDialogType.NegativeButton | FilContentDialogType.PositiveButton
            negativeText: qsTr("Cancel")
            positiveText: qsTr("OK")
            onPositiveClicked: {
                Qt.openUrlExternally("https://gitee.com/time--chicken/SecAssistPro/releases/latest")
            }
        }
        NetworkCallable {
            id: id_networkcallable
            property bool silent: true
            onStart: {
                console.debug("Start Check Update ...")
            }
            onFinish: {
                console.debug("Check Update Finish")
                FilEventBus.post("checkUpdateFinish")
            }
            onSuccess:
                (result) => {
                    var data = JSON.parse(result)
                    console.log("Current Version " + AppInfo.version)
                    console.log("New Version " + data.tag_name)
                    if (data.tag_name !== AppInfo.version) {
                        id_filcontentdialog__update.newVersion = data.tag_name
                        id_filcontentdialog__update.body = data.body
                        id_filcontentdialog__update.open()
                    } else {
                        if (!silent) {
                            showInfo(qsTr("The current version is already the latest"))
                        }
                    }
            }
            onError:
                (status, errorString)=> {
                    if (!silent) {
                        showError(qsTr("The Network is abnormal"))
                    }
                    console.debug(status + ";" + errorString)
            }
        }
        function checkUpdate(silent) {
            id_networkcallable.silent = silent
            Network.get("https://gitee.com/api/v5/repos/time--chicken/SecAssistPro/releases/latest")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0")
            .bind(control)
            .go(id_networkcallable)
        }
        Component.onCompleted: {
            checkUpdate(true)
        }
    }
    FilFrame {
        Layout.fillWidth: true
        Layout.rightMargin: control.rightScrollableMargin
        height: 100
        padding: 10
        color: "transparent"
        ColumnLayout {
            spacing: 5
            FilText {
                text: qsTr("Qt Version")
                font: FilTextStyle.BodyStrong
                Layout.bottomMargin: 4
            }
            FilText {
                text: AppInfo.buildQtVersion
            }
        }
    }
    FilFrame {
        Layout.fillWidth: true
        Layout.rightMargin: control.rightScrollableMargin
        height: 100
        padding: 10
        color: "transparent"
        ColumnLayout {
            spacing: 5
            FilText {
                text: qsTr("Compile Time")
                font: FilTextStyle.BodyStrong
                Layout.bottomMargin: 4
            }
            FilText {
                text: "%1 %2".arg(AppInfo.buildDate).arg(AppInfo.buildTime)
            }
        }
    }
}
